﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RedfieldWeather
{
	public partial class Graphs : System.Web.UI.Page
	{
		private DateTime _displayDate = new DateTime(0);

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!this.IsPostBack)
				LoadData();
		}

		private void LoadData()
		{
			if (_displayDate.Ticks == 0)
				_displayDate = DateTime.Now;

			imgGraph.ImageUrl = string.Format("http://www.wunderground.com/cgi-bin/wxStationGraphAll?day={0}&year={1}&month={2}&ID=KMINILES3&type=3&width=500&showsolarradiation=1&showuv=1&showtemp=1&showpressure=1&showwind=1&showwinddir=1&showrain=1", _displayDate.Day, _displayDate.Year, _displayDate.Month);

			btnBack.Text = String.Format("< {0:d}", _displayDate.AddDays(-1));
			btnRight.Text = String.Format("{0:d} >", _displayDate.AddDays(1));

			hfDisplayDate.Value = _displayDate.ToString("yyyy-MM-dd");
		}

		protected void btnBack_Click(object sender, EventArgs e)
		{
			btnToday.Visible = true;
			btnRight.Visible = true;

			_displayDate = Convert.ToDateTime(hfDisplayDate.Value).AddDays(-1);
			LoadData();
		}

		protected void btnToday_Click(object sender, EventArgs e)
		{
			btnToday.Visible = false;
			btnRight.Visible = false;
			LoadData();
		}

		protected void btnRight_Click(object sender, EventArgs e)
		{
			_displayDate = Convert.ToDateTime(hfDisplayDate.Value).AddDays(1);

			if (_displayDate.Date >= DateTime.Now.Date)
				btnToday_Click(sender, e);
			else
				LoadData();
		}
	}
}